package com.ruoyi.user.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaCodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import com.ruoyi.common.core.cg.constant.RedisConstant;
import com.ruoyi.common.core.cg.dto.AddCommunityDto;
import com.ruoyi.common.core.cg.pojo.Community;
import com.ruoyi.common.core.cg.pojo.Order;
import com.ruoyi.common.core.cg.pojo.User;

import com.ruoyi.common.core.cg.result.Result;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.user.mapper.UserMapper;
import com.ruoyi.user.service.CommunityService;
import com.ruoyi.user.service.UserService;
import io.lettuce.core.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;
    @Autowired
    private CommunityService communityService;


    /**
     * 登录
     * @param loginUser
     * @return
     */
    @Override
    public Result login(User loginUser) {
        // 查询用户是否存在
        User user = mapper.findByOpenId(loginUser.getOpenId());
        Map<String,Object> data = new HashMap<>();
        if (user == null){
            // 不存在则注册
            mapper.addUser(loginUser);
//            mapper.addUserCommunity(loginUser.getUserId());
            data.put("userId",loginUser.getUserId());
            data.put("newUser",true);
            return new Result(true,"登录成功",data);
        }
        data.put("userId",user.getUserId());
        return new Result(true,"登录成功",data);
    }

    /**
     * 添加用户对应的订单
     * @param userId
     * @param oid
     */
    @Override
    public void addUserOrder(Integer userId, Integer oid) {
        mapper.addUserOrder(userId,oid);
    }

    /**
     * 得到用户所属的团
     * @param userId
     * @return
     */
    @Override
    public Integer getUserGroup(Integer userId) {
        return mapper.getUserGroup(userId);
    }

    /**
     * 用户进入拼团点
     * @param dto
     */

    @Override
    public void joinCommunity(AddCommunityDto dto) {
        Community newCommunity = new Community();
        newCommunity.setQqMapId(dto.getQqMapId());
        newCommunity.setTitle(dto.getTitle());
        newCommunity.setAddress(dto.getAddress());
        newCommunity.setLat(dto.getLat());
        newCommunity.setLng(dto.getLng());
        Integer userId = dto.getUserId();
        // 查询拼团点
        Community community = communityService.findCommunityByQQmapId(dto.getQqMapId());
        // 如果不存在拼团点则添加拼团点
        if (null == community){
            communityService.addCommunity(newCommunity);
            // 用户关联拼团点
            mapper.addUserCommunity(userId,newCommunity.getId());
        }else {
            // 如果存在则修改拼团点人数
            communityService.updateCommunityMember(dto.getQqMapId());
            mapper.addUserCommunity(userId,community.getId());

        }
    }

    /**
     * 查询用户未提货或者已提货的订单个数
     * @param userId
     * @param state
     * @return
     */
    @Override
    public Integer getUserOrderByState(Integer userId,Integer state) {
        return mapper.getUserOrderByState(userId,state);
    }

    /**
     * 更新用户预留名称与预留电话
     * @param name 预留名称
     * @param phone 预留电话
     * @param userId 用户ID
     */

}
